package com.qijia.travel.app;

import java.util.List;

import com.qijia.travel.common.SqlMappedId;
import com.qijia.travel.dao.BaseDAO;
import com.qijia.travel.dao.SingletonDaoFactory;
import com.qijia.travel.dao.TravelDbException;
import com.qijia.travel.domain.AgencyMission;
import com.qijia.travel.domain.PageResult;
import com.qijia.travel.domain.TouristGroup;
import com.qijia.travel.domain.example.AgencyMissionExample;
import com.qijia.travel.domain.example.ExampleFactory;
import com.qijia.travel.domain.example.TouristGroupExample;

public class GroupManager {
	private BaseDAO baseDao;

	public GroupManager() {
		try {
			baseDao = SingletonDaoFactory.createDAO();
		} catch (TravelDbException e) {
			e.printStackTrace();
		}
	}

	public AgencyMission getAgencyMissionById(int agencyMissionId)
			throws AppErrorException {
		AgencyMission agencyMission = new AgencyMission();
		agencyMission.setAgencyMissionId(agencyMissionId);

		try {
			return (AgencyMission) baseDao.selectByPrimaryKey(
					SqlMappedId.AGENCY_MISSION + "."
							+ SqlMappedId.selectByPrimaryKey, agencyMission);
		} catch (TravelDbException e) {
			throw new AppErrorException("获取团社基本信息失败!");
		}
	}

	@SuppressWarnings("unchecked")
	public AgencyMission getAgencyMissionByName(String agencyMissionName)
			throws AppErrorException {
		AgencyMission agencyMission = new AgencyMission();
		try {
			AgencyMissionExample example = ExampleFactory
					.createAgencyMissionExample();
			example.createCriteria().andAgencyMissionNameEqualTo(
					agencyMissionName);
			List<AgencyMission> dataList = (List<AgencyMission>) baseDao
					.selectByExample(SqlMappedId.AGENCY_MISSION + "."
							+ SqlMappedId.selectByExample, example);

			if (dataList != null && dataList.size() > 1) {
				throw new AppErrorException("存在相同的团社名称,请指定具体的团社ID号!");
			} else if (dataList != null && dataList.size() == 1)
				return dataList.get(0);
			else
				return agencyMission;
		} catch (TravelDbException e) {
			throw new AppErrorException("获取团社基本信息失败!");
		}
	}

	@SuppressWarnings("unchecked")
	public int createAgencyMission(AgencyMission agencyMission)
			throws AppErrorException {
		if (agencyMission == null
				|| agencyMission.getAgencyMissionName() == null)
			throw new AppErrorException("请输入新增的团社的基本信息");
		try {
			AgencyMissionExample example = ExampleFactory
					.createAgencyMissionExample();
			example.createCriteria().andAgencyMissionNameEqualTo(
					agencyMission.getAgencyMissionName().trim());
			List<AgencyMission> dataList = (List<AgencyMission>) baseDao
					.selectByExample(SqlMappedId.AGENCY_MISSION + "."
							+ SqlMappedId.selectByExample, example);

			if (dataList != null && dataList.size() > 0)
				return dataList.get(0).getAgencyMissionId();

			agencyMission.setAgencyMissionId(baseDao
					.getAutoIncrementId(SqlMappedId.AGENCY_MISSION));
			baseDao.insert(SqlMappedId.AGENCY_MISSION + "."
					+ SqlMappedId.insert, agencyMission);

			return agencyMission.getAgencyMissionId();
		} catch (TravelDbException e) {
			throw new AppErrorException("注册新团社失败,请稍候重试!", e);
		}
	}

	public void updateAgencyMission(AgencyMission agencyMission)
			throws AppErrorException {
		if (agencyMission == null)
			throw new AppErrorException("请选择要进行修改的团社");
		try {
			baseDao.updateByPrimaryKeySelective(SqlMappedId.AGENCY_MISSION
					+ "." + SqlMappedId.updateByPrimaryKeySelective,
					agencyMission);
		} catch (TravelDbException e) {
			e.printStackTrace();
			throw new AppErrorException("更新选中的团社["
					+ agencyMission.getAgencyMissionName() + "]失败,请稍候重试!", e);
		}
	}

	public void deleteAgencyMission(int agencyMissionId)
			throws AppErrorException {
		if (agencyMissionId == 0)
			throw new AppErrorException("请选择要进行删除的团社");
		AgencyMission agencyMission = new AgencyMission();
		try {
			agencyMission.setAgencyMissionId(agencyMissionId);
			baseDao.deleteByPrimaryKey(SqlMappedId.AGENCY_MISSION + "."
					+ SqlMappedId.deleteByPrimaryKey, agencyMission);
		} catch (TravelDbException e) {
			throw new AppErrorException("删除选中的团社[编号为:"
					+ agencyMission.getAgencyMissionId() + "]失败,请稍候重试!", e);
		}
	}

	@SuppressWarnings("unchecked")
	public List<AgencyMission> getAgencyMissionByExample()
			throws AppErrorException {
		AgencyMissionExample example = ExampleFactory
				.createAgencyMissionExample();
		example.createCriteria();
		try {
			return (List<AgencyMission>) baseDao.selectByExample(
					SqlMappedId.AGENCY_MISSION + "."
							+ SqlMappedId.selectByExample, example);
		} catch (TravelDbException e) {
			throw new AppErrorException("查询指定条件下的团社失败,请稍候重试!", e);
		}
	}

	public PageResult selectAgencyMissionList(int curPage)
			throws AppErrorException {
		AgencyMissionExample example = ExampleFactory
				.createAgencyMissionExample();
		example.createCriteria();
		example.setOrderByClause(" agency_mission_id ");

		try {
			PageResult page = PageResult.newInstance();
			page.setSelectDataMappedId(SqlMappedId.AGENCY_MISSION + "."
					+ SqlMappedId.selectByExample);
			page.setCountMappedId(SqlMappedId.AGENCY_MISSION + "."
					+ SqlMappedId.countByExample);
			page.setSqlParamObj(example);

			if (curPage <= 1)
				curPage = 1;

			page.setCurPage(curPage);
			page.setPageSize(10);

			return baseDao.selectByPage(page);
		} catch (TravelDbException e) {
			throw new AppErrorException("获取团社数据列表失败,请稍候重试!", e);
		} catch (InstantiationException e) {
			throw new AppErrorException("获取团社数据列表失败,请稍候重试!", e);
		} catch (IllegalAccessException e) {
			throw new AppErrorException("获取团社数据列表失败,请稍候重试!", e);
		}
	}

	// TODO
	public TouristGroup getTouristGroupById(int touristGroupId)
			throws AppErrorException {
		TouristGroup touristGroup = new TouristGroup();
		touristGroup.setTouristGroupId(touristGroupId);

		try {
			return (TouristGroup) baseDao.selectByPrimaryKey(
					SqlMappedId.TOURIST_GROUP + "."
							+ SqlMappedId.selectByPrimaryKey, touristGroup);
		} catch (TravelDbException e) {
			throw new AppErrorException("获取旅游团基本信息失败!");
		}
	}

	@SuppressWarnings("unchecked")
	public Integer getTouristGroupBySn(String touristGroupSn)
			throws AppErrorException {
		TouristGroupExample example = ExampleFactory
				.createTouristGroupExample();
		example.createCriteria().andTouristGroupSnEqualTo(touristGroupSn);
		try {
			List<TouristGroup> groups = (List<TouristGroup>) baseDao
					.selectByExample(SqlMappedId.TOURIST_GROUP + "."
							+ SqlMappedId.selectByExample, example);

			if (groups != null && groups.size() > 0) {
				return groups.get(0).getTouristGroupId();
			} else
				return null;
		} catch (TravelDbException e) {
			throw new AppErrorException("查询指定条件下的旅游团失败,请稍候重试!", e);
		}
	}

	@SuppressWarnings("unchecked")
	public void createTouristGroup(TouristGroup touristGroup)
			throws AppErrorException {
		if (touristGroup == null || touristGroup.getTouristGroupSn() == null
				|| touristGroup.getAgencyMissionId() == null
				|| touristGroup.getAgencyMissionId() == 0)
			throw new AppErrorException("请输入新增的旅游团的基本信息");
		try {

			TouristGroupExample example = ExampleFactory
					.createTouristGroupExample();
			example.createCriteria().andTouristGroupSnEqualTo(
					touristGroup.getTouristGroupSn().trim());
			List<TouristGroup> dataList = (List<TouristGroup>) baseDao
					.selectByExample(SqlMappedId.TOURIST_GROUP + "."
							+ SqlMappedId.selectByExample, example);

			if (dataList == null || dataList.size() <= 0)
				baseDao.insert(SqlMappedId.TOURIST_GROUP + "."
						+ SqlMappedId.insert, touristGroup);

		} catch (TravelDbException e) {
			throw new AppErrorException("注册新旅游团失败,请稍候重试!", e);
		}
	}

	public void updateTouristGroup(TouristGroup touristGroup)
			throws AppErrorException {
		if (touristGroup == null || touristGroup.getTouristGroupId() == null)
			throw new AppErrorException("请选择要进行修改的旅游团");
		try {
			baseDao.updateByPrimaryKeySelective(SqlMappedId.TOURIST_GROUP + "."
					+ SqlMappedId.updateByPrimaryKeySelective, touristGroup);
		} catch (TravelDbException e) {
			e.printStackTrace();
			throw new AppErrorException("更新选中的旅游团["
					+ touristGroup.getTouristGroupSn() + "]失败,请稍候重试!", e);
		}
	}

	public void deleteTouristGroup(int touristGroupId) throws AppErrorException {
		if (touristGroupId == 0)
			throw new AppErrorException("请选择要进行删除的旅游团");
		TouristGroup touristGroup = new TouristGroup();
		try {
			touristGroup.setTouristGroupId(touristGroupId);
			baseDao.deleteByPrimaryKey(SqlMappedId.TOURIST_GROUP + "."
					+ SqlMappedId.deleteByPrimaryKey, touristGroup);
		} catch (TravelDbException e) {
			throw new AppErrorException("删除选中的旅游团[编号为:"
					+ touristGroup.getTouristGroupId() + "]失败,请稍候重试!", e);
		}
	}

	@SuppressWarnings("unchecked")
	public List<TouristGroup> getTouristGroupByExample()
			throws AppErrorException {
		TouristGroupExample example = ExampleFactory
				.createTouristGroupExample();
		example.createCriteria();
		try {
			return (List<TouristGroup>) baseDao.selectByExample(
					SqlMappedId.TOURIST_GROUP + "."
							+ SqlMappedId.selectByExample, example);
		} catch (TravelDbException e) {
			throw new AppErrorException("查询指定条件下的旅游团失败,请稍候重试!", e);
		}
	}

	public PageResult selectTouristGroupList(int curPage)
			throws AppErrorException {
		TouristGroupExample example = ExampleFactory
				.createTouristGroupExample();
		example.createCriteria();
		example.setOrderByClause(" agency_mission_id desc ");

		try {
			PageResult page = PageResult.newInstance();
			page.setSelectDataMappedId(SqlMappedId.TOURIST_GROUP + "."
					+ SqlMappedId.selectByExample);
			page.setCountMappedId(SqlMappedId.TOURIST_GROUP + "."
					+ SqlMappedId.countByExample);
			page.setSqlParamObj(example);

			if (curPage <= 1)
				curPage = 1;

			page.setCurPage(curPage);
			page.setPageSize(10);

			return baseDao.selectByPage(page);
		} catch (TravelDbException e) {
			throw new AppErrorException("获取旅游团数据列表失败,请稍候重试!", e);
		} catch (InstantiationException e) {
			throw new AppErrorException("获取旅游团数据列表失败,请稍候重试!", e);
		} catch (IllegalAccessException e) {
			throw new AppErrorException("获取旅游团数据列表失败,请稍候重试!", e);
		}
	}
}
